﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio.DataAccess;

public partial class Stock_Stock : System.Web.UI.Page, IStockView
{

    #region Fields
    StockPresenter _presenter;
    #endregion
    #region Properties
    public IEnumerable<Branch> Branches
    {
        private get;
        set;
    }

    public IEnumerable<SP_GetProductsByBranch_Result> BranchProducts
    {
        set
        {
            this.GridView1.DataSource = value;
            this.GridView1.DataBind();
        }
    }

    public short SelectedBranch
    {
        get { return Convert.ToInt16(this.ddlBranch.SelectedValue); }
    }

    public IEnumerable<Product> Products
    {
        private get;
        set;
    }

    public IEnumerable<Provider> Providers
    {
        private get;
        set;
    }

    public long Product
    {
        get
        {
            var ddl = (DropDownList)DetailsView1.FindControl("ddlProducts");
            return Convert.ToInt64(ddl.SelectedValue);
        }
    }

    public int Provider
    {
        get
        {
            var ddl = (DropDownList)DetailsView1.FindControl("ddlProviders");
            return Convert.ToInt32(ddl.SelectedValue);
        }
    }

    public int Quantity
    {
        get
        {
            var txt = (TextBox)DetailsView1.FindControl("txtQuantity");
            return Convert.ToInt32(txt.Text);
        }
    }

    #endregion

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        _presenter = new StockPresenter(this);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this._presenter.GetBranches();
        if (!IsPostBack)
        {
            ddlBranch.DataSource = this.Branches;
            ddlBranch.DataBind();
            _presenter.GetProducts();
        }
    }


    protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
    {
        _presenter.GetProducts();
    }


    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        pnlProviders.Visible = true;
        this._presenter.GetAllProducts();
        this._presenter.GetProviders();

        var ddl = (DropDownList)DetailsView1.FindControl("ddlProducts");
        ddl.DataSource = this.Products;
        ddl.DataBind();

        ddl = (DropDownList)DetailsView1.FindControl("ddlProviders");
        ddl.DataSource = this.Providers;
        ddl.DataBind();

    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values.Add("BranchId", this.SelectedBranch);
        e.Values.Add("ProductId", this.Product);
        e.Values.Add("ProviderId", this.Provider);
        e.Values.Add("Quantity", this.Quantity);
        e.Values.Add("Date", DateTime.Now.Date);
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        this._presenter.AddStock();
        pnlProviders.Visible = false;
        _presenter.GetProducts();
    }
}